import { defineStore } from "pinia";
import { Cookies } from "quasar";
import { usePermissionStore } from "./permission";
import { postAction } from "src/utils/requestAction";
import { i18n } from "src/boot/i18n";

export const useUserStore = defineStore("user", {
  state: () => ({
    token: undefined,
    empeeAcct: undefined,
    staffName: undefined,
    staffId: undefined,
    password: undefined,
    rememberMe: true,
    language: undefined,
  }),
  getters: {},
  actions: {
    async HandleLogin(loginForm) {
      const res = await postAction("public/login", loginForm);
      if (res.code === 1) {
        this.password = loginForm.password;
        const token = res.data.token;
        const empeeAcct = res.data.empee_acct;
        const staffName = res.data.staff_name;
        const staffId = res.data.staff_id;
        this.SetToken(token);
        this.empeeAcct = empeeAcct;
        Cookies.set("dc-empeeAcct", empeeAcct);
        this.staffName = staffName;
        Cookies.set("dc-staffName", staffName);
        this.staffId = staffId;
        Cookies.set("dc-staffId", staffId);
        Cookies.set("dc-language", "zh-CN");
        this.language = "zh-CN";
        return true;
      } else {
        return;
      }
    },
    SetToken(token) {
      this.token = token;
      Cookies.set("dc-token", token);
      localStorage.setItem("dc-token", token);
    },
    ChangeRememberMe(type) {
      this.rememberMe = type;
    },
    HandleLogout() {
      const permissionStore = usePermissionStore();
      permissionStore.ClearMenu();
      localStorage.removeItem("dc-token");
      Cookies.remove("dc-token");
      Cookies.remove("dc-empeeAcct");
      Cookies.remove("dc-staffName");
      Cookies.remove("dc-staffId");
      Cookies.remove("dc-language");
      this.token = undefined;
      this.empeeAcct = undefined;
      this.staffName = undefined;
      this.staffId = undefined;
      this.language = undefined;
      this.password = undefined;
    },
    GetToken() {
      if (localStorage.getItem("dc-token")) {
        return localStorage.getItem("dc-token");
      } else if (Cookies.get("dc-token")) {
        return Cookies.get("dc-token");
      } else {
        return this.token;
      }
    },
    GetEmpeeAcct() {
      if (this.empeeAcct) {
        return this.empeeAcct;
      } else if (Cookies.get("dc-empeeAcct")) {
        return Cookies.get("dc-empeeAcct");
      } else {
        return i18n.global.t("FallbackEmpeeAcct");
      }
    },
    GetStaffName() {
      if (this.staffName) {
        return this.staffName;
      } else if (Cookies.get("dc-staffName")) {
        return Cookies.get("dc-staffName");
      } else {
        return i18n.global.t("FallbackUsername");
      }
    },
    GetStaffId() {
      if (this.GetStaffId) {
        return this.GetStaffId;
      } else if (Cookies.get("dc-staffId")) {
        return Cookies.get("dc-staffId");
      } else {
        return i18n.global.t("FallbackStaffId");
      }
    },
    GetLanguage() {
      if (this.language) {
        return this.language;
      } else if (Cookies.get("dc-language")) {
        return Cookies.get("dc-language");
      } else {
        return "zh-CN";
      }
    },
    // not legit when password does not contains at least one number and one letter
    GetIfPasswordLegit() {
      if (!this.password) {
        return true;
      } else if (
        this.password &&
        !this.password.match(/(?=.*[0-9])(?=.*[a-zA-Z]).{8,30}/)
      ) {
        return false;
      }
      return true;
    },
    SetPassword(password) {
      this.password = password;
    },
  },
  persist: {
    enabled: true,
  },
});
